<?php

namespace app\controller\front;

use app\BaseController;
use app\model\UserCert as UserCertModel;
use app\model\Certificate as CertificateModel;
use think\facade\Db;

class UserCert extends BaseController
{

    public function getUserCerts()
    {
        $params = request()->param();

        $res = UserCertModel::where('user_id = '.$params['uid'])->select();

        foreach ($res as $key => &$item) {
            $item['certInfo'] = CertificateModel::find($item['cert_id']);
        	$item['category'] = $item['certInfo']['category'];
        }

        return $this->successMessage('查询成功',$res);
    }

    // 员工证书地图
    public function getCertificate()
    {
        $params = request()->param();

        // 所有证书
        $certificateModel = new CertificateModel();
        $res = $certificateModel->select(); // 证书数组

        // 查出来个人获得的证书
        $userCertModel = new UserCertModel();
        $userCert = $userCertModel->where(['user_id' => $params['uid']] )->select();

//        print_r(json_decode($userCert));
//        exit;

        // 返回得到证书的时间数组
        $dateArray = array_column(json_decode($userCert), 'get_time');

        $jsonCert = json_decode($userCert);

        foreach ($res as $k => $v) {
            $v['obtain'] =  0;
            $v['date'] = '';
            foreach ($userCert as $key => $item) {
                if ($item['cert_id'] == $v['id']) {
                    $v['obtain'] += 1;
                    $v['date'] = $v['date'].$item['get_time'].';';
                }

            }
        }

        return $this->successMessage('获取成功', $res);
    }

}